package com.itheima.controller.sms;

import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.sms.SmsFlashPromotion;
import com.itheima.service.sms.SmsFlashPromotionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/flash")
public class SmsFlashPromotionController {

    @DubboReference
    private SmsFlashPromotionService flashPromotionService;

    /**
     * 添加活动
     * @param flashPromotion
     * @return
     */
    @PostMapping("/create")
    private R createFlash(@RequestBody SmsFlashPromotion flashPromotion){
        flashPromotionService.save(flashPromotion);
        return R.success();
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R deleteFlash(@PathVariable Integer id){
        flashPromotionService.removeById(id);
        return R.success();
    }

    /**
     * 根据活动名称分页查询
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R pageFlash(@RequestParam(required = false) String keyword,
                       @RequestParam(required = false) Integer pageNum,
                       @RequestParam(required = false) Integer pageSize){
        PageInfo pageInfo = flashPromotionService.pageFlash(keyword,pageNum,pageSize);
        return R.success(pageInfo);
    }

    /**
     * 修改上下线状态
     * @param status
     * @param id
     */
    @PostMapping("/update/status/{id}")
    public R updateStatus(@RequestParam Integer  status, @PathVariable Integer id){
        flashPromotionService.updateStatus(status, id);
        return R.success();
    }

    /**
     * 编辑活动
     * @param flashPromotion
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateFlash(@RequestBody SmsFlashPromotion flashPromotion){
        flashPromotionService.updateById(flashPromotion);
        return R.success();
    }

    /**
     * 获取活动详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getFlashDetails(@PathVariable Integer id ){
        SmsFlashPromotion flashPromotion = flashPromotionService.getById(id);
        return R.success(flashPromotion);
    }

}
